<template>
  <div class="index-container">
    <div class="main-header clearfix">
      <el-row>
        <el-col :xs="8"
                :sm="8"
                :md="8"
                :lg="8"
                :xl="8">
          <el-button type="primary"
                     icon="el-icon-plus"
                     class="sureBtn"
                     size="medium"
                     @click="handleAdd"> 新增记录</el-button>
        </el-col>
        <el-col :xs="16"
                :sm="16"
                :md="16"
                :lg="16"
                :xl="16">
          <div class="search fr">
            <div>
              <el-input v-model="searchForm.title"
                        size="medium"
                        placeholder="老人姓名"></el-input>
            </div>
            <div>
              <el-input v-model="searchForm.code"
                        size="medium"
                        placeholder="批准文号"></el-input>
            </div>
            <div>
              <el-select v-model="searchForm.stock"
                         clearable
                         placeholder="库存"
                         filterable
                         size="medium"
                         style="width: 100%;">
                <el-option label="正常"
                           value="1"></el-option>
                <el-option label="无库存"
                           value="2"></el-option>
              </el-select>
            </div>
            <div style="margin-right: 5px;">
              <el-checkbox v-model="searchForm.guoqi"
                           true-label="1">即将过期药品</el-checkbox>
            </div>
            <el-button size="medium"
                       type="primary"
                       icon="el-icon-search"
                       class="fr"
                       @click="handleSearch"> 搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="main-content"
         style="height: calc(100vh - 250px);">
      <main-table :columns="tableColumns"
                  :data="tableList"
                  :loading="loading"
                  :pagination="pagination"
                  wrapper-height="calc(100% - 73px)"
                  height="100%">
        <el-table-column slot="action"
                         fixed="right"
                         label="操作"
                         align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="danger"
                       @click="edit(scope.row)">删除</el-button>
          </template>
        </el-table-column>

      </main-table>
    </div>

    <el-dialog title="新增记录"
               :visible.sync="dialogVisible"
               :close-on-click-modal="false"
               width="500px">
      <el-form ref="ruleForms"
               :model="ruleForms"
               :rules="rules"
               label-width="100px"
               class="demo-ruleForm">
        <el-form-item label="药品名称"
                      prop="name">
          <el-input v-model="ruleForms.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="submitForm('ruleForms')">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="提示"
               :visible.sync="dialogVisibleRemove"
               width="500px">
      <span>删除记录将同步删除库存数据，
        确定要删除记录吗？</span>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisibleRemove = false">取 消</el-button>
        <el-button type="primary"
                   @click="remove">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import mainTable from "@/components/main-table/index";
import * as APIMedicalCare from "@/api/medicalCareDisease";
export default {
  name: "Remove",
  components: {
    mainTable,
  },
  data() {
    return {
      loading: false,
      title: "添加图标",
      dialogVisible: false,
      dialogVisibleRemove: false,
      ruleForms: {
        name: "",
      },
      id: "",
      rules: {
        name: [{ required: true, message: "清输入名称", trigger: "blur" }],
      },
      ruleForm: {},
      searchForm: {
        page: 1,
      },
      tableList: [],
      pagination: {
        current: 1,
        size: 10,
        total: 0,
      },
      tableColumns: [
        {
          index: true,
          fixed: true,
        },
        {
          prop: "name",
          label: "药品名称",
          align: "center",
        },
        {
    prop: "create_time",
    label: "添加时间",
    align: "center",
    formatter: (row, column, cellValue) => {
      const timestamp = parseInt(cellValue) * 1000; // 将时间戳转换为毫秒
      const date = new Date(timestamp);
      const year = date.getFullYear(); // 提取年份（例如：2023）
      const month = String(date.getMonth() + 1).padStart(2, "0"); // 提取月份，并补零（例如：09）
      const day = String(date.getDate()).padStart(2, "0"); // 提取日期，并补零（例如：01）
      return `${year}-${month}-${day}`; // 返回年月日格式（例如：2023-09-01）
    },
  },
        {
          slot: "action",
        },
      ],
    };
  },
  mounted() {
    this.fetchTableList();
  },
  methods: {
    handleSearch() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0,
      };
      this.fetchTableList({ page: 1 });
    },
    fetchTableList() {
      this.loading = true;
      APIMedicalCare.stockList(this.searchForm)
        .then((resp) => {
          if (resp.code == 200) {
            const { list = [], total = 0 } = resp.data;
            this.tableList = list || [];
            console.log(this.tableList);
            this.pagination.total = total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    edit(row) {
      this.dialogVisibleRemove = true;
      this.id = row.id;
      console.log(row);
    },
    handleAdd() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          APIMedicalCare.addStockList(this.ruleForms).then((res) => {
            if (res.code == 200) {
              this.ruleForms = {
                name: "",
              };
              this.fetchTableList();
              this.$message.success("添加成功");
              this.dialogVisible = false;
            }
          });
        } else {
          this.ruleForms = {
                name: "",
              };
          this.$message("清输入正确的信息");
        }
      });
    },
    remove() {
      this.loading = true;
      let params = {
        id:this.id
      }
      APIMedicalCare.deleteStockList(params).then((res) => {
        this.dialogVisibleRemove = false;
        this.id = "";
        this.loading = false;
        this.fetchTableList();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.index-container {
  box-sizing: border-box;
  width: 100%;
  padding: 0 30px !important;
  margin: 0 !important;
  background: #fff !important;

  .main-header {
    padding-top: 4px;
    margin-bottom: 24px;

    .search {
      display: inline-block;

      .search-title {
        flex: 0 0 48px;
        width: 48px;
        font-size: 16px;
        line-height: 36px;
        color: #333;
      }
    }

    .search > div {
      display: inline-block;
      margin-left: 20px;
    }
  }
  .task-icon-img {
    width: 80px;
    height: 80px;
  }
}

.form-footer {
  padding-left: 100px;
}

.dialog-main {
  .table-do {
    margin-bottom: 15px;
  }

  ::v-deep {
    th {
      .cell {
        color: #fff;
      }
    }
  }
}
</style>
